﻿// Autore: Mattia Barrasso

using System.Linq;
using NUnit.Framework;
using RecipesManager.Data;
using TechTalk.SpecFlow;

namespace RecipesManager.Specs.Ingrediente
{
    [Binding,Scope(Feature = "CreaNuovoIngrediente")]
    public class AggiungiIngredienti_Steps : ReceipeManagerBdd
    {
        #region

        [BeforeScenario]
        public new void SetUpScenario()
        {
            base.SetUpScenario();
        }

        [AfterScenario]
        public new void TearDownScenario()
        {
            base.TearDownScenario();
        }

        #endregion

        [Given(Regex = @"esistono le categorie")]
        public void DateCategorieEsistenti(Table table)
        {
            foreach (TableRow row in table.Rows)
            {
                if (row["Attributi"].Equals("Nome"))
                {
                    Manager.Categories.Add(row["Valori"]);
                }
                else
                {
                    Assert.Fail(@"Tabella mal formata");
                }
            }
        }

        [Given(Regex = @"esistono i negozi")]
        public void DatiNegoziEsistenti(Table table)
        {
            foreach (TableRow row in table.Rows)
            {
                if (row["Attributi"].Equals("Nome"))
                {
                    Manager.Shops.Add(row["Valori"]);
                }
                else
                {
                    Assert.Fail(@"Tabella mal formata");
                }
            }
        }

        [Given(Regex = @"esiste l'ingrediente (.+)")]
        public void DatiIngredientiEsistenti(string ingrediente)
        {
            Manager.Ingredients.Add(ingrediente, MeasureType.Weight);
            //Assert.That(Manager.Ingredients.Contains(ingrediente));
        }

        [Given(Regex = @"l'ingrediente (.+) non esiste")]
        public void DatoIngredienteNonEsistente(string ingrediente)
        {
            Assert.That(!Manager.Ingredients.Contains(ingrediente));
        }

        [Given(Regex = @"ho inserito un ingrediente")]
        public void DatoIngredienteInserito(Table table)
        {
            NavigateToIngredients();
            base.GetIgredientsUi();
            _creaIngrediente.Click();
            base.GetSingleIngredientUi();
            foreach (var row in table.Rows)
            {
                if (row["Attributi"].Equals("Nome"))
                {
                    _ingredientName.SetValue(row["Valori"]);
                }
                else if (row["Attributi"].Equals("Unità"))
                {
                    switch (row["Valori"])
                    {
                        case "Peso":
                            _unitButtons[0].Click();
                            break;
                        case "Volume":
                            _unitButtons[1].Click();
                            break;
                        case "Unità":
                            _unitButtons[2].Click();
                            break;
                        default:
                            Assert.Fail("Unità");
                            break;
                    }
                }
                else if (row["Attributi"].Equals("Categoria"))
                {
                    Assert.That(Manager.Categories.Contains(row["Valori"]));
                    _categoriesBox.Click();
                    _categoriesBox.Select(row["Valori"]);
                    _addCategory.Click();
                }
                else if (row["Attributi"].Equals("Negozio"))
                {
                    Assert.That(Manager.Shops.Contains(row["Valori"]));
                    _shopsBox.Click();
                    _shopsBox.Select(row["Valori"]);
                    _addShop.Click();
                }
                else
                {
                    Assert.Fail("Table");
                }
            }
        }

        [When(Regex = @"salvo")]
        public void QuandoSalvo()
        {
            _saveButton.Click();
        }

        [When(Regex = @"visiono le potenziali categorie")]
        public void QuandoVisionoLePotenzialiCategorie()
        {
            _categoriesBox.Click();
        }

        [When(Regex = @"visiono i potenziali negozi")]
        public void QuandoVisionoIPotenzialiNegozi()
        {
            _shopsBox.Click();
        }

        [Then(Regex = @"l'operazione ha successo")]
        public void AlloraSuccesso()
        {
            Assert.That(MainWindow.ModalWindows().Count == 0);
        }

        [Then(Regex = @"l'operazione non ha successo")]
        public void AlloraInsuccesso()
        {
            Assert.That(MainWindow.ModalWindows().Count != 0);
        }

        [Then(Regex = @"l'ingrediente ([a-zA-Z]+) esiste")]
        public void AlloraIngredienteEsiste(string ingrediente)
        {
            Assert.That(_ingredients.Items.Any(item => item.Text.Equals(ingrediente)));
            Assert.That(Manager.Ingredients.Contains(ingrediente));
        }

        [Then(Regex = @"l'ingrediente ([a-zA-Z]+) non esiste")]
        public void AlloraIngredienteNonEsiste(string ingrediente)
        {
            Assert.That(!_ingredients.Items.Any(item => item.Text.Equals(ingrediente)));
            Assert.That(!Manager.Ingredients.Contains(ingrediente));
        }

        [Then(Regex = @"esistono ([0-9]+) ingredienti")]
        public void AlloraEsistonoNIngredienti(int nIngredienti)
        {
            Assert.That(_ingredients.Items.Count == nIngredienti);
            Assert.That(Manager.Ingredients.Count == nIngredienti);
        }

        [Then(Regex = @"la lista contiene ([0-9]+) negozi")]
        public void AlloraLaListaContieneNNegozi(int nNegozi)
        {
            Assert.That(_shopsBox.Items.Count-1 == nNegozi);
        }

        [Then(Regex = @"la lista contiene ([0-9]+) categorie")]
        public void AlloraLaListaContieneNCategorie(int nCategorie)
        {
            Assert.That(_categoriesBox.Items.Count-1 == nCategorie);
        }

    }
}